*****************************************************************************
*               Cleaning and Analyzing - Switching vote Germany                  *
*  																            *
* Author: 			Valentina Gonzalez-Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			March 21 2022 									*
* Version:			Stata 17						                        *
*																			*
*****************************************************************************
/*
This do-file:
	* Processing of the data
		* Call the Data
		* Define variables
		* Save the data
	* Load preapred data **line 869**
	* Analysis and Descriptives: Export Tables &  Figure

Input: GSS data
	- Data\Switching\pgen.dta
	- Figure/US.gph // this will be used to merge with German.gph (i.e you should run first 1_1_Switching_US.do)

Final output:
	Cleaned data: 
		* "Data\SOEP.dta" this data contains the relevant variables for the analysis.
	Tables:
		* table A4: Switching Vote (Only left) - Germany, IV - RTI [Table\SDU.tex]
		* table A7: Switching Vote From Establishment Left and Right to Populist Right, IV - RTI, German [Table\SOEPlong_2.tex]
		* table A8: Switching Vote, IV - Routine (dummy), Germany [Table\SOEPdummy_2.tex]
		* table A9: Switching Vote (Only from the Right), IV - RTI [Table\CDU.tex]
	Figure:
	    * Figure 3: The effect of exposure to automation on vote-switching. [German Part]  [Figure/Germany.gph]
		* Figure 3: The effect of exposure to automation on vote-switching. [Merging gph US and German part] [Figure/Graph_US_Germany.pdf]


*/
** Set directory
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"

*##########################################
* Processing of the data (alternatively skip and go to line 869)
*##########################################
{
************************************************************************
**************** A. Calling the data  **********************************
************************************************************************
{

* Calling the data
use "Data\not_for_dataverse\pgen.dta", clear


keep if syear==2014 | syear==2018 // since elections 2013 vs 2017


}


************************************************************************
********************* B. Demographic  **********************************
************************************************************************
{
* Dummy for born outside the US
gen foreign=.
replace foreign=0 if pgnation==1
replace foreign=. if pgnation<1
replace foreign=1 if pgnation>1 & pgnation~=.

* Dummy for nonreligion
gen nonrelig=1
replace nonrelig=0 if plh0258_h>0 & plh0258_h<12
replace nonrelig=. if plh0258_h<0

gen educ=pgbilzeit
replace educ=. if pgbilzeit<0
replace educ=. if pgbilzeit>40

* Dummy for high level of education
gen high=(educ>13) // 13 as cut off because 13.5 is the 75% percentile

* Dummy for female
gen female =. 
replace female = 1 if sex==2
replace female = 0 if sex==1

* Calculating age
destring syear gebjahr, replace
gen age = . 
replace age = syear-gebjahr if gebjahr>0 & gebjahr~=. 
replace age=. if syear<0 | syear==. 
replace age=. if gebjahr<0

* Dummy for unemployed
gen unemployed=. 
replace unemployed=1 if pglfs ==6
replace unemployed=0 if pglfs ~=6 
replace unemployed=. if pglfs <0

gen income=. 
replace income=plb0471_h
replace income=. if plb0471_h<0 | plb0471_h==. 


gen rincome = . 
replace rincome=1 if income<1000
replace rincome=2 if income<2999 & income>999
replace rincome=3 if income<3999 & income>3000
replace rincome=4 if income<4999 & income>4000
replace rincome=5 if income<5999 & income>5000
replace rincome=6 if income<6999 & income>6000
replace rincome=7 if income<7999 & income>7000
replace rincome=8 if income<9999 & income>8000
replace rincome=9 if income<15000 & income>10000
replace rincome=10 if income<20000 & income>14999
replace rincome=11 if income<25000 & income>19999
replace rincome=12 if income>24999 & income~=.

}


************************************************************************
********************* C. Parties  **************************************
************************************************************************
{
* Populist [only Afd]
gen populist=. 
replace populist = 1 if plh0012_h==27 //Alternative fur Deutschland (AfD)
replace populist = 0 if plh0012_h==26 //Piratenpartei
replace populist = 0 if plh0012_h==7 //NDP, Republicans, The Right
replace populist = 0 if plh0012_h~=27 &  plh0012_h~=26 & plh0012_h~=7  
replace populist = . if plh0012_h<1



gen establishment_left2=. 
replace establishment_left2 = 1 if plh0012_h==1 //soc social democratic - spd
replace establishment_left2 = 1 if plh0012_h==4 //[4] FDP	lib liberal
replace establishment_left2 = 0 if plh0012_h~=1 & plh0012_h~=4 
replace establishment_left2 = . if plh0012_h<1

gen establishment_right=. 
replace establishment_right = 1 if plh0012_h==2 
replace establishment_right = 1 if plh0012_h==3 
replace establishment_right = 0 if plh0012_h~=2 & plh0012_h~=3  
replace establishment_right = . if plh0012_h<1

}

************************************************************************
********************* D. Standarizing occupations **********************
************************************************************************
{
gen year=syear
gen iscoco = pgisco88
replace iscoco = . if pgisco88<0
replace iscoco = p_isco88 if pgisco88<0
replace iscoco = . if iscoco<0

foreach var of varlist iscoco  {
gen str_`var'=string(`var')
replace str_`var'="0100" if str_`var'=="100"
}
sum iscoco if str_iscoco=="0100"
foreach x in iscoco {
gen `x'2 = substr(str_`x',1,2) if `x'<9334
replace `x'2 = "." if `x'2==""
}

drop str_*

destring iscoco2, replace

label var iscoco2 "ISCO88 at the 2-digit"

* Labels Iscoco
{
gen iscoco2n="."
replace iscoco2n="Legislators and senior officials" if iscoco2==11
	replace iscoco2n="Corporate managers" if iscoco2==12
	replace iscoco2n="General managers" if iscoco2==13
	replace iscoco2n="Physical, mathematical and engineering science professionals" if iscoco2==21
	replace iscoco2n="Life science and health professionals" if iscoco2==22
	replace iscoco2n="Teaching professionals" if iscoco2==23
	replace iscoco2n="Other professionals" if iscoco2==24
	replace iscoco2n="Physical and engineering science associate professionals" if iscoco2==31
	replace iscoco2n="Life science and health associate professionals" if iscoco2==32
	replace iscoco2n="Teaching associate professionals" if iscoco2==33
	replace iscoco2n="Other associate professionals" if iscoco2==34
	replace iscoco2n="Office clerks" if iscoco2==41
	replace iscoco2n="Customer services clerks" if iscoco2==42
	replace iscoco2n="Personal and protective services workers" if iscoco2==51
	replace iscoco2n="Models, salespersons and demonstrators" if iscoco2==52
	replace iscoco2n="Market-oriented skilled agricultural and fishery workers" if iscoco2==61
	replace iscoco2n="Subsistence agricultural and fishery workers" if iscoco2==62
	replace iscoco2n="Extraction and building trades workers" if iscoco2==71
	replace iscoco2n="Metal, machinery and related trades workers" if iscoco2==72
	replace iscoco2n="Precision, handicraft, printing and related trades workers" if iscoco2==73
	replace iscoco2n="Other craft and related trades workers" if iscoco2==74
	replace iscoco2n="Stationary-plant and related operators" if iscoco2==81
	replace iscoco2n="Machine operators and assemblers" if iscoco2==82
	replace iscoco2n="Drivers and mobile-plant operators" if iscoco2==83
	replace iscoco2n="Sales and services elementary occupations" if iscoco2==91
	replace iscoco2n="Agricultural, fishery and related labourers" if iscoco2==92
	replace iscoco2n="Labourers in mining, construction, manufacturing and transport" if iscoco2==93
	replace iscoco2n="Armed forces" if iscoco2==01
label var iscoco2n "Names of iscoco2 coding"
}
************************************************************************
********************* Exposure to automation ***************************
************************************************************************
{
************************************************************************
* 1. Goos et al.(2014)
************************************************************************
{
** These lines include the routinisation and offshoring indices at the 2-digit level from Goos et al. (2014)

*/ The idea is that for each occupation (at the 2-digit level) one risk is assigned. The weak part of this index is that it treats as the same the last 2-digit of the ISCO.
* In other words, ISCO has 4 digit, but goos et al only consider the first 2 digit, therefore there is no differentiation among occupations that differ in the last 2 digit. 
gen rti=.

{
replace rti=-0.75 if iscoco2==12
replace rti=-0.82 if iscoco2==21
replace rti=-1.00 if iscoco2==22
replace rti=-0.73 if iscoco2==24
replace rti=-1.52 if iscoco2==13
replace rti=-0.40 if iscoco2==31
replace rti=-0.44 if iscoco2==34
replace rti=-0.33 if iscoco2==32

replace rti=0.32 if iscoco2==81
replace rti=0.46 if iscoco2==72
replace rti=-1.50 if iscoco2==83
replace rti=2.24 if iscoco2==41
replace rti=1.59 if iscoco2==73
replace rti=-0.19 if iscoco2==71
replace rti=1.41 if iscoco2==42
replace rti=0.49 if iscoco2==82
replace rti=1.24 if iscoco2==74

replace rti=0.45 if iscoco2==93
replace rti=-0.60 if iscoco2==51
replace rti=0.05 if iscoco2==52
replace rti=0.03 if iscoco2==91
}

label var rti "RTI index"

}
************************************************************************
***** 2. Oesch (2006) and Kitschelt and Rehm (2014) classification *****
************************************************************************
{
*Now is the same, but using Oesch (2006) and Kitschelt and Rehm (2014) classification

*/  Oesch (2006) develops the bases of a new class schema that partly shifts its focus from hierarchical divisions to horizontal cleavages. The idea is  that the middle class is not conceptualized as a unitary grouping and the manual/non-manual divide is not used as a decisive class boundary. 
*/ The emphasis is put on differences in marketable skills and the work logic. 

// What is the logic? 
*/Logic of task structures: t1 organizational (taskorg), t2 technical (tasktech), and t3 interpersonal (taskinter)
*/Logic of authority: a1 professional (authprof), a2 associate professional (authassoc), a3 skilled routine (authskil) a4 unskilled routine (authunsk)
*/Leads to 3*4 groups, in regressions of K&R combined to 4 groups (skilled+unskilled routine all tasks; prof+assoc prof for 3 tasks separately)

* Code from Thewissen and Rueda  (2019)
{
*/t1a1: Higher grade managers
gen t1a1=1 if iscoco>=1000 & iscoco<=1251 | iscoco>=2410 & iscoco<=2419 | inlist(iscoco,2441,2470)
label var t1a1 "Higher grade managers"

*/t1a2: Associate managers
gen t1a2=1 if iscoco>=1252 & iscoco<=1319 | iscoco>=3410 & iscoco<=3449 | inlist(iscoco,3452)
label var t1a2 "Associate managers"

*/t1a3: Skilled office
gen t1a3=1 if iscoco>=4000 & iscoco<=4112 | iscoco>=4114 & iscoco<=4141 | inlist(iscoco,4143) | iscoco>=4190 & iscoco<=4210 | iscoco>=4213 & iscoco<=4221
label var t1a3 "Skilled office"

*/t1a4: Routine office
gen t1a4=1 if inlist(iscoco,4113,4142,4144) | iscoco>=4211 & iscoco<=4212 | iscoco>=4222 & iscoco<=4223
label var t1a4 "Routine office"

*/t2a1: Technical experts
gen t2a1=1 if iscoco>=2100 & iscoco<=2213
label var t2a1 "Technical experts"

*/t2a2: Technicians
gen t2a2=1 if iscoco>=3100 & iscoco<=3213 | inlist(iscoco,3471)
label var t2a2 "Technicians"

*/t2a3: Skilled crafts
gen t2a3=1 if inlist(iscoco,110,8311,8324,8333) | iscoco>=7120 & iscoco<=7142 | iscoco>=7200 & iscoco<=7233 | iscoco>=7240 & iscoco<=7423 | iscoco>=7430 & iscoco<=7520
label var t2a3 "Skilled crafts"

*/t2a4: Routine operatives/agriculture
gen t2a4=1 if inlist(iscoco,7143, 7234, 7424, 8312) | iscoco>=7100 & iscoco<=7113 | iscoco>=7129 & iscoco<=7130 | iscoco>=8000 & iscoco<=8310 | iscoco>=8334 & iscoco<=8400 | iscoco>=9160 & iscoco<=9162 | iscoco>=9300 & iscoco<=9333
replace t2a4=1 if iscoco>=6010 & iscoco<=6210 | iscoco>=8330 & iscoco<=8332 | iscoco>=9200 & iscoco<=9213
label var t2a4 "Routine operatives/agriculture"

*/t3a1: Socio-cultural professionals
gen t3a1=1 if inlist(iscoco, 2359, 2445, 2451, 2460) | iscoco>=2220 & iscoco<=2323 | iscoco>=2350 & iscoco<=2351 |iscoco>=2420 & iscoco<=2440 | iscoco>=2442 & iscoco<=2443
label var t3a1 "Socio-cultural professionals"

*/t3a2: Socio-cultural semi-professionals
gen t3a2=1 if inlist(iscoco,2352, 2444, 3220, 3226) | iscoco>=2330 & iscoco<=2340 | iscoco>=2446 & iscoco<=2450 | iscoco>=2452 & iscoco<=2455 | iscoco>=3222 & iscoco<=3224 | iscoco>=3229 & iscoco<=3232 | iscoco>=3240 & iscoco<=3400 | iscoco>=3450 & iscoco<=3451 | iscoco>=3460 & iscoco<=3470 | iscoco>=3472 & iscoco<=3480
label var t3a2 "Socio-cultural semi-professionals"

*/t3a3: Skilled service
gen t3a3=1 if inlist(iscoco, 3221, 3225, 5122, 5141, 5143, 8323) | iscoco>=3227 & iscoco<=3228 | iscoco>=5110 & iscoco<=5113 | iscoco>=5150 & iscoco<=5163 | iscoco>=5200 & iscoco<=5210
label var t3a3 "Skilled service"

*/t3a4: Routine service
gen t3a4=1 if inlist(iscoco,5142, 5149, 5169) | iscoco>=5120 & iscoco<=5121 | iscoco>=5123 & iscoco<=5130 | iscoco>=5131 & iscoco<=5140 | iscoco>=5220 & iscoco<=5230 | iscoco>=8320 & iscoco<=8322 | iscoco>=9100 & iscoco<=9153
label var t3a4 "Routine service"


forvalues i=1(1)3 {
	gen t`i'=1 if t`i'a1==1 | t`i'a2==1  | t`i'a3==1 | t`i'a4==1
	}
list t1-t3 if t1==t2 & t2==t3 & t1==t3 & t1~=.
replace t1=0 if t2==1 | t3==1
replace t2=0 if t1==1 | t3==1
replace t3=0 if t1==1 | t2==1

label var t1 "Organisational task structure (t1a1, 2, 3, or 4==1)"
label var t2 "Technical task structure (t2a1, 2, 3, or 4==1)"
label var t3 "Interpersonal task structure (t3a1, 2, 3, or 4==1)"

forvalues i=1(1)4 {
	gen a`i'=1 if t1a`i'==1 | t2a`i'==1  | t3a`i'==1
	}
replace a1=0 if a2==1 | a3==1 | a4==1
replace a2=0 if a1==1 | a3==1 | a4==1
replace a3=0 if a1==1 | a2==1 | a4==1
replace a4=0 if a1==1 | a2==1 | a3==1

label var a1 "Professional authority (t1a1, t2a1, or t3a1==1)"
label var a2 "Assoc prof authority (t1a2, t2a2, or t3a2==1)"
label var a3 "Skilled routine authority (t1a3, t2a3, or t3a3==1)"
label var a4 "Unskilled routine authority (t1a4, t2a4, or t3a4==1)"

gen check = a1+a2+a3+a4
sum check
drop check

gen c1=1 if t1a1==1 | t1a2==1
gen c2=1 if t2a1==1 | t2a2==1
gen c3=1 if t3a1==1 | t3a2==1
gen c4=1 if t1a3==1 | t1a4==1 | t2a3==1 | t2a4==1 | t3a3==1 | t3a4==1

replace c1=0 if c2==1 | c3==1 | c4==1
replace c2=0 if c1==1 | c3==1 | c4==1
replace c3=0 if c1==1 | c2==1 | c4==1
replace c4=0 if c1==1 | c2==1 | c3==1

label var c1 "Skilled organisational (t1a1 or t1a2==1)"
label var c2 "Skilled technical (t2a1 or t2a2==1)"
label var c3 "Skilled interpersonal (t3a1 or t3a2==1)"
label var c4 "Unskilled routine (t1a3, t1a4, t2a3, t2a4, t3a3, or t3a4==1)"

gen check = c1+c2+c3+c4
sum check
drop check
}
}
*********************************************************************
***** 3. Offshoring index from Walter (2017), based on Blinder*******
*********************************************************************
{
*/ Stefanie Walter (2017) provides the code for offshorability based on Blinder. It uses ISCO with 4 digits. 

gen offshwalt=.
label var offshwalt "Offshoring Potential (Blinder) from Walter"


*4-digit ISCO-code
 * Coding is based on the classification developed in Blinder, Alan. 2007. "How Many U.S. Jobs Might Be Offshorable." CEPS Working Paper No. 142.
* NOTE: all professions not listed by Blinder are coded as not offshorable (value 0)
* Unlike Goos et al, Walter considers the 4 digits. 
{
replace offshwalt=0 if iscoco<.
replace offshwalt=49 if iscoco==1142
replace offshwalt=55 if iscoco==1222
replace offshwalt=28 if iscoco==1226
replace offshwalt=55 if iscoco==1228
replace offshwalt=83 if iscoco==1231
replace offshwalt=49 if iscoco==1232
replace offshwalt=40 if iscoco==1233
replace offshwalt=53 if iscoco==1234
replace offshwalt=49 if iscoco==1235
replace offshwalt=55 if iscoco==1236
replace offshwalt=55 if iscoco==1237
replace offshwalt=55 if iscoco==1311
replace offshwalt=55 if iscoco==1312
replace offshwalt=55 if iscoco==1313
replace offshwalt=55 if iscoco==1314
replace offshwalt=55 if iscoco==1315
replace offshwalt=48 if iscoco==1316
replace offshwalt=52 if iscoco==1317
replace offshwalt=55 if iscoco==1318
replace offshwalt=55 if iscoco==1319
replace offshwalt=66 if iscoco==2111
replace offshwalt=74 if iscoco==2112
replace offshwalt=66 if iscoco==2113
replace offshwalt=66 if iscoco==2114
replace offshwalt=89 if iscoco==2121
replace offshwalt=96 if iscoco==2122
replace offshwalt=83 if iscoco==2131
replace offshwalt=90 if iscoco==2139
replace offshwalt=25 if iscoco==2141
replace offshwalt=64 if iscoco==2143
replace offshwalt=74 if iscoco==2144
replace offshwalt=72 if iscoco==2146
replace offshwalt=69 if iscoco==2147
replace offshwalt=86 if iscoco==2148
replace offshwalt=71 if iscoco==2149
replace offshwalt=81 if iscoco==2211
replace offshwalt=83 if iscoco==2212
replace offshwalt=72 if iscoco==2411
replace offshwalt=50 if iscoco==2419
replace offshwalt=74 if iscoco==2421
replace offshwalt=67 if iscoco==2444
replace offshwalt=90 if iscoco==2451
replace offshwalt=78 if iscoco==2452
replace offshwalt=25 if iscoco==2453
replace offshwalt=48 if iscoco==2455
replace offshwalt=47 if iscoco==3111
replace offshwalt=47 if iscoco==3113
replace offshwalt=47 if iscoco==3114
replace offshwalt=72 if iscoco==3115
replace offshwalt=47 if iscoco==3116
replace offshwalt=94 if iscoco==3118
replace offshwalt=54 if iscoco==3119
replace offshwalt=75 if iscoco==3121
replace offshwalt=84 if iscoco==3122
replace offshwalt=68 if iscoco==3123
replace offshwalt=36 if iscoco==3131
replace offshwalt=36 if iscoco==3132
replace offshwalt=46 if iscoco==3133
replace offshwalt=34 if iscoco==3139
replace offshwalt=52 if iscoco==3141
replace offshwalt=60 if iscoco==3152
replace offshwalt=55 if iscoco==3211
replace offshwalt=55 if iscoco==3212
replace offshwalt=44 if iscoco==3213
replace offshwalt=32 if iscoco==3228
replace offshwalt=51 if iscoco==3411
replace offshwalt=85 if iscoco==3412
replace offshwalt=50 if iscoco==3414
replace offshwalt=55 if iscoco==3416
replace offshwalt=59 if iscoco==3419
replace offshwalt=51 if iscoco==3421
replace offshwalt=48 if iscoco==3422
replace offshwalt=38 if iscoco==3431
replace offshwalt=51 if iscoco==3432
replace offshwalt=84 if iscoco==3433
replace offshwalt=84 if iscoco==3434
replace offshwalt=54 if iscoco==3439
replace offshwalt=100 if iscoco==3442
replace offshwalt=85 if iscoco==3471
replace offshwalt=30 if iscoco==3472
replace offshwalt=95 if iscoco==4111
replace offshwalt=94 if iscoco==4112
replace offshwalt=100 if iscoco==4113
replace offshwalt=71 if iscoco==4114
replace offshwalt=38 if iscoco==4115
replace offshwalt=84 if iscoco==4121
replace offshwalt=54 if iscoco==4122
replace offshwalt=31 if iscoco==4131
replace offshwalt=67 if iscoco==4132
replace offshwalt=67 if iscoco==4133
replace offshwalt=46 if iscoco==4141
replace offshwalt=26 if iscoco==4142
replace offshwalt=95 if iscoco==4143
replace offshwalt=54 if iscoco==4144
replace offshwalt=54 if iscoco==4190
replace offshwalt=94 if iscoco==4211
replace offshwalt=54 if iscoco==4214
replace offshwalt=65 if iscoco==4215
replace offshwalt=72 if iscoco==4221
replace offshwalt=54 if iscoco==4222
replace offshwalt=50 if iscoco==4223
replace offshwalt=86 if iscoco==5113
replace offshwalt=59 if iscoco==6142
replace offshwalt=36 if iscoco==7111
replace offshwalt=35 if iscoco==7112
replace offshwalt=36 if iscoco==7113
replace offshwalt=65 if iscoco==7211
replace offshwalt=69 if iscoco==7212
replace offshwalt=70 if iscoco==7213
replace offshwalt=70 if iscoco==7222
replace offshwalt=68 if iscoco==7223
replace offshwalt=68 if iscoco==7224
replace offshwalt=26 if iscoco==7311
replace offshwalt=64 if iscoco==7313
replace offshwalt=69 if iscoco==7321
replace offshwalt=69 if iscoco==7322
replace offshwalt=68 if iscoco==7323
replace offshwalt=68 if iscoco==7324
replace offshwalt=60 if iscoco==7331
replace offshwalt=75 if iscoco==7332
replace offshwalt=59 if iscoco==7341
replace offshwalt=59 if iscoco==7342
replace offshwalt=59 if iscoco==7343
replace offshwalt=34 if iscoco==7344
replace offshwalt=59 if iscoco==7345
replace offshwalt=75 if iscoco==7346
replace offshwalt=68 if iscoco==7413
replace offshwalt=27 if iscoco==7414
replace offshwalt=55 if iscoco==7415
replace offshwalt=43 if iscoco==7421
replace offshwalt=57 if iscoco==7422
replace offshwalt=57 if iscoco==7423
replace offshwalt=66 if iscoco==7424
replace offshwalt=75 if iscoco==7431
replace offshwalt=75 if iscoco==7432
replace offshwalt=75 if iscoco==7433
replace offshwalt=73 if iscoco==7434
replace offshwalt=75 if iscoco==7435
replace offshwalt=75 if iscoco==7436
replace offshwalt=57 if iscoco==7437
replace offshwalt=75 if iscoco==7441
replace offshwalt=72 if iscoco==7442
replace offshwalt=36 if iscoco==8111
replace offshwalt=36 if iscoco==8112
replace offshwalt=36 if iscoco==8113
replace offshwalt=59 if iscoco==8121
replace offshwalt=68 if iscoco==8122
replace offshwalt=70 if iscoco==8123
replace offshwalt=68 if iscoco==8124
replace offshwalt=69 if iscoco==8131
replace offshwalt=68 if iscoco==8139
replace offshwalt=57 if iscoco==8141
replace offshwalt=68 if iscoco==8142
replace offshwalt=68 if iscoco==8143
replace offshwalt=68 if iscoco==8151
replace offshwalt=70 if iscoco==8152
replace offshwalt=68 if iscoco==8153
replace offshwalt=68 if iscoco==8154
replace offshwalt=29 if iscoco==8155
replace offshwalt=68 if iscoco==8159
replace offshwalt=42 if iscoco==8161
replace offshwalt=55 if iscoco==8162
replace offshwalt=29 if iscoco==8163
replace offshwalt=64 if iscoco==8171
replace offshwalt=68 if iscoco==8172
replace offshwalt=68 if iscoco==8211
replace offshwalt=68 if iscoco==8212
replace offshwalt=68 if iscoco==8221
replace offshwalt=35 if iscoco==8222
replace offshwalt=68 if iscoco==8223
replace offshwalt=41 if iscoco==8224
replace offshwalt=29 if iscoco==8229
replace offshwalt=69 if iscoco==8231
replace offshwalt=68 if iscoco==8232
replace offshwalt=57 if iscoco==8240
replace offshwalt=58 if iscoco==8251
replace offshwalt=59 if iscoco==8252
replace offshwalt=68 if iscoco==8253
replace offshwalt=75 if iscoco==8261
replace offshwalt=75 if iscoco==8262
replace offshwalt=75 if iscoco==8263
replace offshwalt=75 if iscoco==8264
replace offshwalt=75 if iscoco==8265
replace offshwalt=75 if iscoco==8266
replace offshwalt=75 if iscoco==8269
replace offshwalt=27 if iscoco==8271
replace offshwalt=68 if iscoco==8272
replace offshwalt=68 if iscoco==8273
replace offshwalt=30 if iscoco==8274
replace offshwalt=31 if iscoco==8275
replace offshwalt=68 if iscoco==8276
replace offshwalt=27 if iscoco==8277
replace offshwalt=68 if iscoco==8278
replace offshwalt=66 if iscoco==8281
replace offshwalt=66 if iscoco==8282
replace offshwalt=66 if iscoco==8283
replace offshwalt=68 if iscoco==8284
replace offshwalt=57 if iscoco==8285
replace offshwalt=64 if iscoco==8286
replace offshwalt=68 if iscoco==8290
replace offshwalt=34 if iscoco==8340
replace offshwalt=95 if iscoco==9113
replace offshwalt=64 if iscoco==9321
replace offshwalt=29 if iscoco==9333
replace offshwalt=55 if iscoco==1227
replace offshwalt=89 if iscoco==2121
replace offshwalt=100 if iscoco==2132
replace offshwalt=70 if iscoco==2145
replace offshwalt=71 if iscoco==2149
replace offshwalt=81 if iscoco==2211
replace offshwalt=46 if iscoco==2412
replace offshwalt=50 if iscoco==2419
replace offshwalt=33 if iscoco==2432
replace offshwalt=89 if iscoco==2441
replace offshwalt=48 if iscoco==2455
replace offshwalt=72 if iscoco==3115
replace offshwalt=54 if iscoco==3119
replace offshwalt=46 if iscoco==3133
replace offshwalt=34 if iscoco==3139
replace offshwalt=34 if iscoco==3224
replace offshwalt=51 if iscoco==3411
replace offshwalt=25 if iscoco==3415
replace offshwalt=50 if iscoco==3417
replace offshwalt=59 if iscoco==3419
replace offshwalt=51 if iscoco==3432
replace offshwalt=54 if iscoco==3439
replace offshwalt=90 if iscoco==3460
replace offshwalt=54 if iscoco==4122
replace offshwalt=31 if iscoco==4131
replace offshwalt=67 if iscoco==4132
replace offshwalt=46 if iscoco==4141
replace offshwalt=26 if iscoco==4142
replace offshwalt=54 if iscoco==4222
replace offshwalt=68 if iscoco==7141
replace offshwalt=68 if iscoco==7224
replace offshwalt=65 if iscoco==7241
replace offshwalt=34 if iscoco==7344
replace offshwalt=72 if iscoco==7442
replace offshwalt=68 if iscoco==8122
replace offshwalt=68 if iscoco==8139
replace offshwalt=42 if iscoco==8161
replace offshwalt=68 if iscoco==8211
replace offshwalt=68 if iscoco==8223
replace offshwalt=41 if iscoco==8224
replace offshwalt=57 if iscoco==8240
replace offshwalt=58 if iscoco==8251
replace offshwalt=75 if iscoco==8269
replace offshwalt=66 if iscoco==8281
replace offshwalt=66 if iscoco==8283
replace offshwalt=68 if iscoco==8284
replace offshwalt=68 if iscoco==8290
replace offshwalt=70 if iscoco==9322
}

* Create Ordinal Offshwalt Variable
{
gen offshwalt4=.
replace offshwalt4=4 if offshwalt<.
replace offshwalt4=3 if offshwalt<75
replace offshwalt4=2 if offshwalt<50
replace offshwalt4=1 if offshwalt<25
label var offshwalt4 "offshwalt ordinal (4 categories)"
}

* Binary Offshwalt Variable
gen offshwalt2=offshwalt4>1
replace offshwalt2=. if offshwalt==.
label var offshwalt2 "offshwalt binary: =1 if offshwalt>=25 if offshwalt"
}
*******************************************************************************
*****4. Skill specificity provided Iversen, Cusack and Rehm (2011) ************
*******************************************************************************
{
{
*/ This code is provided by Torben Iversen; Thomas Cusack; Philipp Rehm, 2011: Risks at Work The Demand and Supply Sides of Government Redistribution
*/ See: Raw data\Occupations\Measuring_skill-specificity Iversen, Cusack and Rehm (2011).xlsx column J Relative skill specificity  
{
gen relskillspec=.	
	replace relskillspec=6.168528055	if iscoco2==11
	replace relskillspec=2.840445869	if iscoco2==12
	replace relskillspec=1.612754672	if iscoco2==13
	replace relskillspec=3.94911483		if iscoco2==21
	replace relskillspec=2.875304662	if iscoco2==22
	replace relskillspec=1.298205499	if iscoco2==23
	replace relskillspec=3.38271403		if iscoco2==24
	replace relskillspec=5.999693379	if iscoco2==31
	replace relskillspec=5.599717306	if iscoco2==32
	replace relskillspec=2.435374975	if iscoco2==33
	replace relskillspec=3.582262408	if iscoco2==34
	replace relskillspec=1.803739154	if iscoco2==41
	replace relskillspec=3.899735492	if iscoco2==42
	replace relskillspec=3.020082744	if iscoco2==51
	replace relskillspec=0.787535801	if iscoco2==52
	replace relskillspec=4.674256488	if iscoco2==61
	replace relskillspec=3.91546829		if iscoco2==71
	replace relskillspec=3.842807254	if iscoco2==72
	replace relskillspec=20.41910075	if iscoco2==73
	replace relskillspec=9.446026838	if iscoco2==74
	replace relskillspec=25.06473567	if iscoco2==81
	replace relskillspec=12.27676336	if iscoco2==82
	replace relskillspec=3.672294237	if iscoco2==83
	replace relskillspec=7.39344664		if iscoco2==91
	replace relskillspec=7.384150506	if iscoco2==92
	replace relskillspec=6.460338111	if iscoco2==93
	label var relskillspec "Relative skill specificity , Iversen"
}
	
}

}

*******************************************************************************
* 5 task categories Kurer (2020)
*******************************************************************************
{
rename iscoco iscoco_withoutchange
gen iscoco=iscoco_withoutchange
    
{
*generating 3 task categories
gen task = .
gen isco=iscoco
replace task = 1 if inlist(isco, 2411, 2431, 2441, 3411, 3471)
replace task = 1 if inrange(isco, 2100, 2213)
replace task = 1 if inrange(isco, 2443, 2444)
replace task = 1 if inrange(isco, 2446, 2452)
replace task = 1 if inrange(isco, 3100, 3212)
replace task = 1 if inrange(isco, 3433, 3440)
replace task = 1 if inrange(isco, 3442, 3444)

replace task = 2 if inlist(isco, 2442, 2445, 3226, 3432, 3441)
replace task = 2 if inrange(isco, 1000, 1319)
replace task = 2 if inrange(isco, 2220, 2410)
replace task = 2 if inrange(isco, 2412, 2430)
replace task = 2 if inrange(isco, 2432, 2440)
replace task = 2 if inrange(isco, 2453, 2470)
replace task = 2 if inrange(isco, 3213, 3220)
replace task = 2 if inrange(isco, 3222, 3224)
replace task = 2 if inrange(isco, 3229, 3410)
replace task = 2 if inrange(isco, 3412, 3429)
replace task = 2 if inrange(isco, 3449, 3470)
replace task = 2 if inrange(isco, 3472, 3480)

replace task = 3 if isco==4223
replace task = 3 if inrange(isco, 3430, 3431)
replace task = 3 if inrange(isco, 4000, 4195)
replace task = 3 if inrange(isco, 4210, 4215)

replace task = 4 if inlist(isco, 7124, 8340, 9120, 9133)
replace task = 4 if inrange(isco, 1, 110) /* departing from oesch, including 110 (armed forces). this is the actual intention of 1-100 */
replace task = 4 if inrange(isco, 6100, 7113)
replace task = 4 if inrange(isco, 7210, 8290)
replace task = 4 if inrange(isco, 9000, 9001)
replace task = 4 if inrange(isco, 9150, 9151)
replace task = 4 if inrange(isco, 9153, 9161)
replace task = 4 if inrange(isco, 9200, 9311)

replace task = 5 if inlist(isco, 5122, 5143, 9002, 9162)
replace task = 5 if inrange(isco, 7120, 7123)
replace task = 5 if inrange(isco, 7129, 7143)
replace task = 5 if inrange(isco, 8300, 8334)
replace task = 5 if inrange(isco, 9130, 9132)
replace task = 5 if inrange(isco, 9140, 9142)
replace task = 5 if inrange(isco, 9312, 9313)

replace task = 6 if inlist(isco, 3221, 3225, 4200, 9152)
replace task = 6 if inrange(isco, 3227, 3228)
replace task = 6 if inrange(isco, 4220, 4222)
replace task = 6 if inrange(isco, 5000, 5121)
replace task = 6 if inrange(isco, 5123, 5142)
replace task = 6 if inrange(isco, 5149, 5220)
replace task = 6 if inrange(isco, 9003, 9005)
replace task = 6 if inrange(isco, 9100, 9113)
replace task = 6 if inrange(isco, 9320, 9333) /* departing from oesch, including 9333 (transport labourers, animal vehicles) */

* add isco categories 2000 and 3000
* officially not defined, thus not part of oesch's categories

replace task = 1 if inlist(isco, 2000, 3000)

* task3

gen task3 = .
replace task3 = 1 if task==1 | task==2 // NRM
replace task3 = 2 if task==3 | task==4 // R
replace task3 = 3 if task==5 | task==6 // NRC
tab task3


drop isco iscoco
rename iscoco_withoutchange iscoco

}

}
}
************************************************************************
* Generate a dummy to summarize the risk of automation 
gen task3cog1=(task3==1) // dummy for non routine cognitive work - 3 task approach
replace task3cog1=. if task3==.
gen task3cog2and3=(task3==2 | task3==3) // dummy for  routine cognitive work - 3 task approach
replace task3cog2and3=. if task3==.

}
************************************************************************
********************* E. Switching Vote ********************************
************************************************************************
{
// Generating the years I am interested
{
gen pres12=. 
replace pres12=1 if establishment_left2==1 & syear==2014
replace pres12=2 if establishment_right==1 & syear==2014
replace pres12=3 if populist==1 & syear==2014
replace pres12=4 if plh0012_h>0 & populist~=1 & establishment_right~=1 & establishment_left2~=1 & syear==2014


gen pres16=. 
replace pres16=1 if establishment_left2==1 & syear==2018
replace pres16=2 if establishment_right==1 & syear==2018
replace pres16=3 if populist==1 & syear==2018
replace pres16=4 if plh0012_h>0 & populist~=1 & establishment_right~=1 & establishment_left2~=1 & syear==2018

gen vot_12_14=pres12 if syear==2014
replace vot_12_14=. if pres12~=1 & pres12~=2 & pres12~=3   & pres12~=4
by pid, sort: egen vot12_14=total(vot_12_14)

gen switching2 = . 
replace switching2 = 1 if vot12_14==1 & pres16==3 // left to pop
replace switching2 = 0 if vot12_14==3 & pres16==3 // always pop
replace switching2 = 0 if vot12_14==2 //Republicans in 2012
replace switching2 = 0 if vot12_14==4 //Other
replace switching2 = 0 if vot12_14==3 & pres16~=3 // Populist to anything except Rep

gen switching2_broad = . 
replace switching2_broad = 1 if vot12_14~=3 & pres16==3 // no pop to pop
replace switching2_broad = 0 if vot12_14==3 & pres16==3 // always pop
replace switching2_broad = 0 if pres16~=3 & syear==2018

gen switching2_r = . 
replace switching2_r = 1 if vot12_14==2 & pres16==3 // left to pop
replace switching2_r = 0 if vot12_14==3 & pres16==3 // always pop
replace switching2_r = 0 if vot12_14==1 //Republicans in 2012
replace switching2_r = 0 if vot12_14==4 //Other
replace switching2_r = 0 if vot12_14==3 & pres16~=3 // Populist to anything except Rep



}
}
*############################################
* Saving the data
*############################################
{
// Label
lab var switching2 "Left to Pop Right"
lab var switching2_r "Right to AfD"
lab var switching2_broad "Switching Vote"
lab var rti "RTI Index"
lab var female "Female"
lab var age "Age"
lab var educ "Education"
lab var high "High-Skilled"
lab var unemployed "Unemployed"
lab var foreign "Foreign born"
lab var nonrelig "Non-Believer"
lab var offshwalt2 "Offshorability"
lab var relskillspec "Skill-Specificity"
lab var t2 "Task-Tech"
lab var t3 "Task-Inter"
lab var task3cog1 "Non-Routine"
lab var task3cog2and3 "Routine"
lab var sampreg "Region (West 1 - East 2)"

	
}
keep switching2 switching2_r switching2_broad rti female age educ high unemployed nonrelig offshwalt2 relskillspec t2 t3 task3cog1 task3cog2and3 sampreg pid syear income rincome year  phrf   foreign

save "Data\SOEP.dta", replace

}
*##########################################
* Alternatively load prepared data
*##########################################
{
use "Data\SOEP.dta", clear	
}	
*##########################################	
* Analysis	
*##########################################	
{	
{	
	
// How is the data?	
xtset pid syear	
xtdes	
	
* Figure	
{	
// Graph style	
// Graph style		
grstyle clear		
set scheme s2color		
grstyle init		
grstyle set plain, nogrid		
grstyle color background white		
		
// Figure 3: The effect of exposure to automation on vote-switching. [German part]		
// Margins 		
{		


	 
logit  switching2   rti  female age  foreign   income offshwalt2      [pw=phrf]	if year==2018 

		
margins, atmeans at(rti=(-1.52(0.05)2.24)) 		
     marginsplot , recast(line) recastci(rline) ci1opts(fintensity(50) lpattern(dot)) xti(Risk of automation (RTI - Index)) yti(Predicted Probability of Switching (95% CI)) ti("Germany") 		 		saving("Figure/Germany.gph", replace)	
}		
   		
graph combine "Figure/US.gph" "Figure/Germany.gph"		
 graph export "Figure/Graph_US_Germany.pdf", as(pdf) replace		
		
}	
					
// Regressions					
{					
// table A4: Switching Vote (Only left) - Germany, IV - RTI					
{					
eststo clear					
eststo: qui logit switching2 rti  female age   foreign high i.rincome   [pw=phrf] 	if year==2018, robust				
eststo: qui logit switching2 rti  female age   foreign high i.rincome offshwalt2  i.sampreg [pw=phrf] 	if year==2018, robust				
eststo: qui logit switching2 rti  female age   foreign high i.rincome offshwalt2 relskillspec  i.sampreg  [pw=phrf] 	if year==2018	, robust			
eststo: qui logit switching2 rti  female age   foreign high i.rincome offshwalt2 relskillspec t2 t3 i.sampreg  [pw=phrf] 	if year==2018	, robust			
esttab , replace label se title(Switching Vote (Only left), IV - RTI \label {tab:left})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age    high offshwalt2 relskillspec t2 t3) scalars(N  r2_p aic) indicate( "Income = *income"  "Regional controls = *.sampreg")					
esttab using "Table\SDU.tex", replace label se title(Switching Vote (Only left), IV - RTI, Germany \label {tab:left})mti("+Demographic" "+Offshoring" "+Skill"  "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age    high offshwalt2 relskillspec t2 t3) scalars( "N Observations" "r2_p R$^2$" "aic AIC" )indicate( "Income = *income"  "Regional controls = *.sampreg")					
}					
					
// table A7: Switching Vote From Establishment Left and Right to Populist Right, IV - RTI, German					
{					
eststo clear					
eststo: qui logit switching2_broad rti  female age   foreign high i.rincome    [pw=phrf] if year==2018	, robust				
eststo: qui logit switching2_broad rti  female age   foreign high i.rincome offshwalt2  i.sampreg [pw=phrf] if year==2018	, robust				
eststo: qui logit switching2_broad rti  female age   foreign high i.rincome offshwalt2 relskillspec  i.sampreg [pw=phrf] if year==2018		, robust			
eststo: qui logit switching2_broad rti  female age   foreign high i.rincome offshwalt2 relskillspec t2 t3 i.sampreg [pw=phrf] if year==2018		, robust			
esttab , replace label se title(Switching Vote From Establishment Left and Right to Populist Right, IV - RTI, Germany \label {tab:Table1rtilongswnarrow2})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign high offshwalt2 relskillspec t2 t3) scalars("N Observations" "r2_p R$^2$" "aic AIC") indicate( "Income = *income"  "Regional controls = *.sampreg")					

esttab using "Table\SOEPlong_2.tex", replace label se title(Switching Vote From Establishment Left and Right to Populist Right, IV - RTI, Germany \label {tab:Table1rtilongswnarrow2})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign high offshwalt2 relskillspec t2 t3) scalars("N Observations" "r2_p R$^2$" "aic AIC") indicate( "Income = *income"  "Regional controls = *.sampreg")					
}					
					
// table A8: Switching Vote, IV - Routine (dummy), Germany					
{					
	eststo clear				
eststo: qui logit switching2_broad task3cog2and3  female age   foreign high i.rincome   [pw=phrf] if year==2018, robust					
					
eststo: qui logit switching2_broad task3cog2and3  female age   foreign high i.rincome offshwalt2  i.sampreg [pw=phrf] if year==2018		, robust			
eststo: qui logit switching2_broad task3cog2and3  female age   foreign high i.rincome offshwalt2 relskillspec i.sampreg [pw=phrf] if year==2018		, robust			
					
eststo: qui logit switching2_broad task3cog2and3  female age   foreign high i.rincome offshwalt2 relskillspec t2 t3 i.sampreg [pw=phrf] if year==2018		, robust			
esttab , replace label se title(Switching Vote, IV - Routine (dummy), Germany \label {tab:Table1rtilongswnarrowtask3cog2and3})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(task3cog2and3  female age   foreign high offshwalt2 relskillspec t2 t3) scalars("N Observations" "r2_p R$^2$" "aic AIC") indicate( "Income = *.rincome"  "Regional controls = *.sampreg")					
					
	esttab  using "Table\SOEPdummy_2.tex", replace label se title(Switching Vote, IV - Routine (dummy), Germany \label {tab:Table1rtilongswnarrowtask3cog2and3})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(task3cog2and3  female age   foreign high offshwalt2 relskillspec t2 t3) scalars("N Observations" "r2_p R$^2$" "aic AIC") indicate( "Income = *.rincome"  "Regional controls = *.sampreg")				
}					
}					
					
// table A9: Switching Vote (Only from the Right), IV - RTI					
	eststo clear				
	eststo: qui logit switching2_r rti  female age   foreign high  i.rincome offshwalt2  i.sampreg [pw=phrf] if year==2018, robust								
eststo: qui logit switching2_r rti  female age   foreign high  i.rincome offshwalt2  i.sampreg [pw=phrf] if year==2018, robust	
eststo: qui logit switching2_r rti  female age   foreign high  i.rincome offshwalt2 relskillspec  i.sampreg [pw=phrf] if year==2018	, robust		
eststo: qui logit switching2_r rti  female age   foreign high  i.rincome offshwalt2 relskillspec t2 t3 i.sampreg [pw=phrf] if year==2018	, robust				
		
esttab , replace label se title(Switching Vote (Only from the Right), IV - RTI \label {tab:CDU})mti("+Demographic" "+Offshoring" "+Skill"  "All") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign high offshwalt2 relskillspec t2 t3) scalars( "N Observations" "r2_p R$^2$" "aic AIC" ) indicate( "Income = *income"  "Regional controls = *.sampreg")					
					
esttab using "Table\CDU.tex", replace label se title(Switching Vote (Only from the Right), IV - RTI, Germany \label {tab:CDU})mti("+Demographic" "+Offshoring" "+Skill"  "All")  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rti  female age   foreign high offshwalt2 relskillspec t2 t3) scalars( "N Observations" "r2_p R$^2$" "aic AIC" )indicate( "Income = *income"  "Regional controls = *.sampreg")					
					
					
}					
}					
*##########################################					
* Descriptive 					
*##########################################					
{					
// table A2: Descriptive statistic: Germany SOEP 2014 vs 2018.					
{			
		eststo clear
		
qui estpost sum switching2_broad   rti age income female  foreign unemployed high  offshwalt2 relskillspec t2 t3 sampreg [w=phrf] if switching2_broad~=., d					
					
esttab  ,  /// ,  ,					
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///				
	nonumber label replace noobs varlabels(distance_redist "Distance Redistribution" distance_div "Distance Diversity" distance_fixed "Distance Fixed Attributes" PRITM "PR with Trichotomous Multipartism" totseats "Total Number of Seats" number2 "Total Number of Parties" oecdmember "OECD member") nomtitle 	
	
esttab using "Table\summarystats_Germany.tex" ,  /// ,  ,					
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///				
	nonumber label replace noobs varlabels(distance_redist "Distance Redistribution" distance_div "Distance Diversity" distance_fixed "Distance Fixed Attributes" PRITM "PR with Trichotomous Multipartism" totseats "Total Number of Seats" number2 "Total Number of Parties" oecdmember "OECD member") nomtitle 				
}					
}					
